package com.itheima.service.impl;

import com.itheima.dao.StudentMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import com.itheima.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * Create By Awen on 2021/8/22
 *
 * @author Awen
 */
public class StudentServiceImpl implements StudentService {
    @Override
    public PageBean<Student> queryStudentsByPage(int currentPage, int pageSize) {
        PageBean<Student> pb = new PageBean<>();
        SqlSession session = SqlSessionUtil.getSqlSession();
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        //总条数
        int totalCount = mapper.queryCountStudents();
        //起始索引
        int startIndex = (currentPage - 1) * pageSize;
        //学生列表
        List<Student> list = mapper.queryStudentsByPage(startIndex,pageSize);
        System.out.println("学生列表 = " + list);
        pb.setList(list);
        pb.setCurrentPage(currentPage);
        pb.setPageSize(pageSize);
        pb.setTotalCount(totalCount);
        session.close();
        return pb;
    }
}
